package fr.paris.sud.com;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.BorderLayout;
import java.io.File;
import java.util.HashMap;

import fr.lri.swingstates.canvas.CElement;
import fr.lri.swingstates.canvas.CImage;
import fr.lri.swingstates.canvas.CStateMachine;
import fr.lri.swingstates.canvas.Canvas;
import fr.paris.sud.com.VirtualGrid;

import java.awt.Component;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JSplitPane;


public class PhotoManagerWindow extends JFrame{
	
	private final static int WIDTH = 1200;
	private final static int HEIGHT = 800;
	public static JSplitPane splitPane;
	public static Canvas imageViewer;
	public static Canvas canvas;
	public static HashMap<Integer,ImageStructure> tableOfImages = new HashMap<Integer,ImageStructure>();
	private static CImage imageDisplayed = null;
	public BoomerangStateMachine bmgSM;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PhotoManagerWindow window = new PhotoManagerWindow();
					window.setBounds(10, 50, WIDTH, HEIGHT);
					window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					window.setTitle("Photo Manager");
					window.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	public PhotoManagerWindow() {
		
		
		/*JTree tree = new JTree();
		getContentPane().add(tree, BorderLayout.WEST);*/
		
		initialize();
		
//		ComponentListener listener = new ComponentAdapter() {		      
//
//		      public void componentResized(ComponentEvent evt) {
//		        Component c = (Component) evt.getSource();
//		        Dimension newSize = c.getSize();
//		        bmgSM.fileSystem.resize(newSize.width-200);
//		      }
//		    };
//		
//		this.getRootPane().addComponentListener(listener);
	}
	
	private void initialize(){
		
		String picturesFolder = System.getProperty("user.home")+"\\Pictures";
		
		
		splitPane = new JSplitPane();
		splitPane.setContinuousLayout(true);
		splitPane.setAlignmentY(Component.CENTER_ALIGNMENT);
		splitPane.setAlignmentX(Component.CENTER_ALIGNMENT);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setDividerLocation(350);
		
		//splitPane.setDividerLocation(1.0);
		getContentPane().add(splitPane, BorderLayout.CENTER);
		
		imageViewer = new Canvas();
		imageViewer.setBackground(Color.getHSBColor(0.00f, 0.00f, 0.935f));
		splitPane.setLeftComponent(imageViewer);
		
        
		canvas = new Canvas();
		splitPane.setRightComponent(canvas);
		//splitPane.setDividerLocation(150);
		
		
		IconThumbnail iconImg = new IconThumbnail();
		bmgSM = new BoomerangStateMachine();
	
		
		//number of elements, initial X, initial Y, width of grid element, height of grid element, width of canvas, height of canvas
		bmgSM.fileSystem = new VirtualGrid(0, 5, 15, 128, 128, WIDTH-200, HEIGHT);
						
		FileTree tree = new FileTree(new File(picturesFolder),iconImg, tableOfImages, bmgSM.fileSystem);
		getContentPane().add(tree, BorderLayout.WEST);
		
		bmgSM.createStateMachines();
		canvas.attachSM((CStateMachine)bmgSM.throwInteraction, true);
		canvas.attachSM((CStateMachine)bmgSM.catchInteraction, true);
		canvas.attachSM((CStateMachine)bmgSM.gridSnapInteraction, true);
		
		bmgSM.throwInteraction.addStateMachineListener(bmgSM.catchInteraction);
		bmgSM.catchInteraction.addStateMachineListener(bmgSM.throwInteraction);
		bmgSM.catchInteraction.addStateMachineListener(bmgSM.gridSnapInteraction);
		bmgSM.gridSnapInteraction.addStateMachineListener(bmgSM.catchInteraction);
		
		//canvas.setBackground(Color.getHSBColor(175/255, 0.03f, 0.72f));
		//imageViewer = new Canvas(WIDTH-200, HEIGHT/2);
		//imageViewer.setBackground(Color.getHSBColor(0.00f, 0.00f,0.935f));
		
		
		
		
				
		
		
		
		
		
		
		
		
		//canvas.translateBy(200, 0);
		
		
		//JMenuBar menuBar = new JMenuBar();
		//frmPhotoManager.setJMenuBar(menuBar);
		
		/*// A label for displaying the pictures
		iconImg.photographLabel.setVerticalTextPosition(JLabel.BOTTOM);
		iconImg.photographLabel.setHorizontalTextPosition(JLabel.CENTER);
		iconImg.photographLabel.setHorizontalAlignment(JLabel.CENTER);
		iconImg.photographLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));*/
        
        // We add two glue components. Later in process() we will add thumbnail buttons
        // to the toolbar inbetween thease glue compoents. This will center the
        // buttons in the toolbar.
		//iconImg.buttonBar.add(Box.createGlue());
		//iconImg.buttonBar.add(Box.createGlue());
        
        //getContentPane().add(iconImg.buttonBar, BorderLayout.SOUTH);
        //getContentPane().add(iconImg.canvas, BorderLayout.CENTER);
        
        
       
        
		
        
        // this centers the frame on the screen
		setLocationRelativeTo(null);
        
        // start the image loading SwingWorker in a background thread
        //iconImg.loadimages.execute();
		
	}
	
	public static void DisplayPicture(CElement miniature){
		ImageStructure imageToDisplay = tableOfImages.get(miniature.hashCode());
		
//		System.out.println("splitPane.getDividerLocation(): "+splitPane.getDividerLocation());
		
//		System.out.println("imageViewer.getWidth(): "+imageViewer.getWidth()+" imageViewer.getHeight(): "+imageViewer.getHeight());
		imageDisplayed = imageViewer.newImage(1, 1,	IconThumbnail.loadWholeImageToDisplay(imageToDisplay.imagePath));
		imageDisplayed.translateTo(imageViewer.getWidth()/2,imageViewer.getHeight()/2);
	
	}
	
}
